package package1;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.rmi.NoSuchObjectException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.rmi.CORBA.Stub;
import javax.rmi.CORBA.Tie;
import javax.rmi.CORBA.UtilDelegate;
import javax.rmi.CORBA.ValueHandler;

import org.apache.http.HttpConnection;
import org.omg.CORBA.ORB;
import org.omg.CORBA.SystemException;
import org.omg.CORBA.portable.InputStream;
import org.omg.CORBA.portable.OutputStream;

public class TestForInteractionWithOperationSystem {
	public static void main(String[] args) throws IOException {
		// getOperationSystemProcess();
		// showOpeartionSystemProcess();
		// turnOffProcess("notepad.exe");
		signInAndSignOutReminder();
		// System.err.println("123");
		autoSignInAndSignOut();
	}

	private static void autoSignInAndSignOut() throws IOException {
//	 	http://10.10.10.46:8080/sse-spm/sys/biz/workload/uajax/doSign.html?comments=&_=1460099904490
		String url = "http://10.10.10.46:8080/sse-spm/sys/biz/workload/uajax/doSign.html?comments=&_="+(new Date().getTime() + "");
		String cmd = "start "+ url;
		BufferedWriter bw = new BufferedWriter(new FileWriter(new File(getDesktop()+"\\autoSign.bat")));
		bw.write(cmd);
		bw.close();
		// DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// String time=format.format(date);
//		
//		Process p = run(cmd);
//		BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
	}

	private static String getDesktop() {
		return System.getenv("USERPROFILE") + "\\desktop";
	}
	
	private static void signInAndSignOutReminder() throws IOException {
		 String cmd = "@echo off\r\nset/p a=sign in and sign out submit your working time\r\nstart http://10.10.10.46:8080/sse-spm/sys/default.html";
		 FileWriter fw = new FileWriter(new
		 File("c:/users/user/desktop/sign in and sign out.bat"));
		 fw.write(cmd);
		 fw.close();
		 SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 String TimeString = time.format(new java.util.Date());
	}

	private static void showOpeartionSystemProcess() throws IOException {
		String cmd = "tasklist /svc";
		Process p = run(cmd);
		BufferedReader processBR = new BufferedReader(
				new InputStreamReader(new BufferedInputStream(p.getInputStream()), Charset.forName("gb2312")));
		BufferedReader errorBR = new BufferedReader(
				new InputStreamReader(new BufferedInputStream(p.getErrorStream()), Charset.forName("gb2312")));
		String process;
		while ((process = processBR.readLine()) != null)
			System.err.println(process);
		String error;
		if ((error = errorBR.readLine()) != null) {
			System.out.println("\nError Info");
			System.out.println(error);
		}
	}

	private static void turnOffProcess(String processName) throws IOException {
		String cmd = "taskkill -f -im " + processName;
		run(cmd);
	}

	private static Process run(String cmd) throws IOException {
		return Runtime.getRuntime().exec(cmd);
	}

	private static void getOperationSystemProcess() throws IOException {
		ProcessBuilder pb = new ProcessBuilder("tasklist");
		Process p = pb.start();
		BufferedReader processBR = new BufferedReader(
				new InputStreamReader(new BufferedInputStream(p.getInputStream()), Charset.forName("gb2312")));
		BufferedReader errorBR = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getErrorStream())));
		System.out.println("Window 系统进程列表");
		String process;
		while ((process = processBR.readLine()) != null)
			System.out.println(process);
		String error = errorBR.readLine();
		if (error != null) {
			System.out.println("\nError Info");
			System.out.println(error);
		}
	}

}
